//
//  RTJRiskResultController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/10.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJRiskResultController.h"
#import "RTJRiskAssessmentController.h"
#import "RTJUserInfoController.h"

@interface RTJRiskResultController ()
@property (weak, nonatomic) IBOutlet UILabel *riskType;
@property (weak, nonatomic) IBOutlet UILabel *riskContennt;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;

@end

@implementation RTJRiskResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
     self.viewTop.constant = screenH == 812 ? 91 : 71;
    self.title = @"评测结果";
    [_btn setTitle:[RTJUserTool getTheUserModel].is_risk == 1 ? @"重新评测" : @"去投资" forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    if ([RTJUserTool getTheUserModel].is_risk == 1&&_isReply==NO) {
        
        [SVProgressHUD  showWithStatus:nil];
        
        [RTJNetWork asyncRequestWithURL:@"user/user_risk" params:nil method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            [SVProgressHUD dismiss];
            
            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    self.riskType.text = data[@"data"][@"type"];
                    self.riskContennt.text = data[@"data"][@"description"];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

            }
            
        }];
        
    }else{
        
         [self loadData];
    }
    
}

-(void)loadData{
    
    
    [SVProgressHUD  showWithStatus:nil];
    
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/risk_calculation" params:@{@"data":_jsonStr} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        [self.view removeEmptyView];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.riskType.text = data[@"data"][@"result"];
                self.riskContennt.text = data[@"data"][@"description"];
                
                RTJUserModel *model  = [RTJUserTool getTheUserModel];
                model.is_risk = 1;
                [RTJUserTool saveUserInfo:model];
                
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }
        
    }];
}


- (IBAction)risk:(id)sender {
    
    if ([RTJUserTool getTheUserModel].is_risk == 1) {
        
        RTJRiskAssessmentController *vc = [[RTJRiskAssessmentController alloc]init];
        vc.isReply = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

-(void)back{
    
    if ([RTJUserTool getTheUserModel].is_risk == 1) {
        
        for (UIViewController *controller in self.navigationController.viewControllers) {

            if ([controller isKindOfClass:[RTJUserInfoController class]]) {

                RTJUserInfoController *vc = (RTJUserInfoController *)controller;

                [self.navigationController popToViewController:vc animated:YES];

            }

        }
        
//        [self.navigationController popToRootViewControllerAnimated:YES];

        
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

@end
